package com.heima.article.controller.v1;

import com.heima.article.service.ArticleBehaviorService;
import com.heima.article.service.ArticleHomeService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.LoadArticleBehaviorVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-09-12 15:24
 */
@RestController
@RequestMapping("/api/v1/article")
//@CrossOrigin("*")
public class ArticleController {

    @Autowired
    private ArticleHomeService articleHomeService;

    @Autowired
    private ArticleBehaviorService articleBehaviorService;

    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto articleHomeDto){
        List<ApArticle> list = articleHomeService.load(articleHomeDto,null,true);
        return ResponseResult.okResult(list);
    }

    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto articleHomeDto){
        List<ApArticle> list = articleHomeService.load(articleHomeDto, ArticleConstants.LOADTYPE_LOAD_MORE,false);
        return ResponseResult.okResult(list);
    }

    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto articleHomeDto){
        List<ApArticle> list = articleHomeService.load(articleHomeDto,ArticleConstants.LOADTYPE_LOAD_NEW,false);
        return ResponseResult.okResult(list);
    }

    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto){

        if(dto.getAuthorId() == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LoadArticleBehaviorVO vo = articleBehaviorService.loadArticleBehavior(dto);
        return ResponseResult.okResult(vo);
    }
}
